angular.module('wytapp',['ng','ngRoute','ngAnimate'])
  .controller('index',function($scope,$location,$rootScope){
    $rootScope.toit=function(route){
      $location.path(route);
      $rootScope.lists=false;
      //console.log("toit:"+$rootScope.lists);
      //console.log(route);
    };
  })
  .controller('start',function($scope,$rootScope,$location){

  })
  .controller('mains',function($scope,$rootScope,$location,$http){
    $scope.start=5;
    $rootScope.more=function(){
      $http.get("data/dish_list.php?start="+$scope.start)
        .success(function(data){
          $scope.data=$scope.data.concat(data);
          //console.log($scope.data);
        })
      $scope.start+=5;
      $scope.showli=true;
    };

    $scope.key='';
    $scope.$watch('key',function(){
      if($scope.key!==""){
        $http.get("data/dish_search.php?key="+$scope.key)
          .success(function(data){
            $scope.data=data;
            $scope.start=10;
            $scope.showli=true;
            //console.log($scope.data);
          })
      }else{
        $http.get("data/dish_list.php")
          .success(function(data){
            $scope.data=data;
            $scope.start=5;
            $scope.showli=false;
          })
      }
    })
  })
  .controller('detail',function($scope,$location,$rootScope,$routeParams,$http){
    $rootScope.did=$routeParams.did;
    $http.get("data/dish_getbyid.php?did="+$rootScope.did)
      .success(function(data){
        $scope.v=data;
      });
  })
  .controller('order',function($scope,$location,$rootScope,$http,$routeParams){
    $rootScope.did=$routeParams.did;
    $scope.sex=1;
    /*todo:也以对象形式收集信息，发送post请求，需该header发送信息模式*/
    //$scope.information={"did":$rootScope.did};

    $scope.smt=function(){
      $http.get("data/order_add.php?did="+$rootScope.did+"&user_name="+$scope.user_name+"&sex="+$scope.sex+"&phone="+$scope.phone+"&addr="+$scope.addr)
        .success(function(data){
          $rootScope.emg=data.emg;
          $rootScope.ddh=data.ddh;
          $rootScope.toit("myorder");
          $rootScope.lists=true;
          console.log("smt:"+$rootScope.lists);
          console.log($scope.phone);
          $rootScope.utel=$scope.phone;
        });
    }
  })
  .controller('myorder',function($scope,$location,$rootScope,$http){
    $scope.dds=function(d){
      if(d){
        $scope.tel=$rootScope.utel;
        $rootScope.lists=false;
      }
      $http.get("data/order_list.php?phone="+$scope.tel)
        .success(function(data){
          data.reverse();
          for($scope.di=0;$scope.di<data.length;$scope.di++){
            $scope.otime=data[$scope.di].order_time-0;
            data[$scope.di].order_time=new Date($scope.otime).toLocaleString();
          }
          $scope.udata=data;
          $scope.chaxun=true;
        });
    }
  })
  .config(function ($routeProvider) {
    $routeProvider
      .when('/start',{
        templateUrl:'tpl/start.html',
        controller:'start'
      })
      .when('/main',{
        templateUrl:'tpl/main.html',
        controller:'mains'
      })
      .when('/detail/:did',{
        templateUrl:'tpl/detail.html',
        controller:'detail'
      })
      .when('/order/:did',{
        templateUrl:'tpl/order.html',
        controller:'order'
      })
      .when('/myorder',{
        templateUrl:'tpl/myorder.html',
        controller:'myorder'
      })
      .otherwise({
        redirectTo:'start'
      })
  })